package com.firefightingiot.firefightingiot.common.base;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.TableField;
import com.firefightingiot.firefightingiot.common.utils.Dict;
import com.firefightingiot.firefightingiot.support.model.json.ValueFilterX;

import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;

public class BasisEntity extends Dict implements Serializable {

    private static final long serialVersionUID = 1L;
    /**
     * 由于MyBatisPlus CRUD会自动把私有字段当作列进行查询<br>
     * 手动过滤父类JSONObject中的map禁止查询<br>
     * map 在此项目中不能被当成字段名
     */
    @TableField(exist = false)
    private Object map;

    @TableField(exist = false)
    private float threshold;

    public void setSet(BasisEntity basisEntity) {
        this.putAll(basisEntity);
    }

    @TableField(exist = false)
    private float loadFactor;

    public String getString(String name) {
        return this.get(name) == null ? null : StrUtil.isNotBlank(this.getStr(name)) ? this.getStr(name) : null;
    }

    public Timestamp getTimestamp(String name) {
        Date date = Convert.toDate(this.get(name), null);
        return this.get(name) == null ? null : (date == null ? null : new Timestamp(date.getTime()));
    }

    public Integer getInteger(String name) {
        return this.get(name) == null ? null : Convert.toInt(this.get(name));
    }

    @Override
    public BigDecimal getBigDecimal(String name) {
        return this.get(name) == null ? null : new BigDecimal(this.get(name).toString());
    }

    public Boolean getBoolean(String name) {
        return this.get(name) == null ? null : Convert.toBool(this.get(name));
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this, ValueFilterX.get());
    }


}
